package com.neu.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.neu.MainActivity;
import com.neu.R;

public class WelcomeActivity extends AppCompatActivity {

    //定义一个按钮对象
    private Button btnIndex;
    //定义一个TextView对象
    private TextView tvInternet;

    private ViewPager viewPager;

    //单选按钮组
    private RadioGroup rdGroup;
    private int[] rds={R.id.rd1,R.id.rd2,R.id.rd3,R.id.rd4,R.id.rd5};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_welcome);
        //检查是否访问过
        checkFirst();
        //初始化组件
        initView();
    }

    private void checkFirst() {
        SharedPreferences  sp=getSharedPreferences("config", Context.MODE_PRIVATE);
        boolean isFirst=sp.getBoolean("isFirst",false);
        if(isFirst){
            Intent intent=new Intent(this,MainActivity.class);
            startActivity(intent);
            finish();
        }
    }

    //初始化组件对象
    private void initView() {
        //获得组件对象
        btnIndex=findViewById(R.id.btnIndex);
        tvInternet=findViewById(R.id.tvInternet);

        rdGroup=findViewById(R.id.rdGroup);

        //按钮的单击事件
        btnIndex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //从WelcomeActivity跳转到MainActivity
               Intent intent=new Intent(WelcomeActivity.this,MainActivity.class);
               startActivity(intent);

            }
        });
        //网络配置的单击事件
        tvInternet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //从WelcomeActivity跳转到MainActivity
                Intent intent=new Intent(WelcomeActivity.this,InternetActivity.class);
                startActivity(intent);
            }
        });

        //
        viewPager=findViewById(R.id.viewPager);
        viewPager.setAdapter(new PagerAdapter() {

            private int[] imgs={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                //创建一个图片对象
                ImageView pic=new ImageView(getApplication());
                //图片居中
                pic.setScaleType(ImageView.ScaleType.CENTER_CROP);
                //添加图片资源
                pic.setImageResource(imgs[position]);
                //将图片添加到容器中
                container.addView(pic);
                return pic;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView((View) object);
            }

            @Override
            public int getCount() {
                return imgs.length;
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return object==view;
            }
        });


        //viewpager事件处理
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //滑到第五张图片
                if(position+1==5){
                    //显示组件
                    btnIndex.setVisibility(View.VISIBLE);
                    tvInternet.setVisibility(View.VISIBLE);
                }else{
                    //隐藏组件
                    btnIndex.setVisibility(View.GONE);
                    tvInternet.setVisibility(View.GONE);
                }

                //选择
                rdGroup.check(rds[position]);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
}